package com.pie4cloud.pie.job.executor.job;

import com.alibaba.fastjson.JSON;
import com.pie4cloud.pie.common.core.constant.SecurityConstants;
import com.pie4cloud.pie.common.core.util.R;
import com.pie4cloud.pie.common.core.util.RedisUtil;
import com.pie4cloud.pie.job.api.consumer.IHanlderConsumer;
import com.pie4cloud.pie.job.api.consumer.annotation.Hanlder;
import com.pie4cloud.pie.job.api.dto.CallBackDto;
import com.pie4cloud.pie.job.api.dto.PlayBillVo;
import com.pie4cloud.pie.job.api.entity.ConfigDecode;
import com.pie4cloud.pie.job.api.entity.DispenseCongfigEntity;
import com.pie4cloud.pie.job.api.entity.PlaybillEntity;
import com.pie4cloud.pie.job.api.entity.ReceivableEntity;
import com.pie4cloud.pie.job.api.feign.RemoteDispenseCongfigService;
import com.pie4cloud.pie.job.executor.entity.DispenseMonitorEntity;
import com.pie4cloud.pie.job.executor.entity.PlayBillMonitorEntity;
import com.pie4cloud.pie.job.executor.service.DispenseToolService;
import com.pie4cloud.pie.job.executor.service.PlayBillMonitorService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
@Hanlder(handler = "playbillHandler")
public class PlayBillHandlerService implements IHanlderConsumer {
	@Autowired
	private PlayBillMonitorService playBillMonitorService;
	@Autowired
	private DispenseToolService dispenseToolService;
	private final RemoteDispenseCongfigService remoteDispenseCongfigService;
	@Autowired
	private RedisUtil redisUtil;

	private static String STATISTICAL_NUM = "STATISTICAL:NUM:";
	@Override
	public R execute(String jobInfoEntity, CallBackDto callBackDto) throws Exception {
		PlaybillEntity playbillEntity= JSON.parseObject(jobInfoEntity,PlaybillEntity.class);
		Date lastTimeLevel=dispenseToolService.getLastTriggerTime(playbillEntity.getJobCron(),new Date(playbillEntity.getTriggerLastTime()));
		this.excuteMonitor(lastTimeLevel,playbillEntity);
		Date nextTimeLevel=dispenseToolService.getNextTriggerTime(playbillEntity.getJobCron(),lastTimeLevel);
		this.excuteMonitor(nextTimeLevel,playbillEntity);
		return R.ok();
	}
	public void excuteMonitor(Date timeLevel,PlaybillEntity playbillEntity){
		PlayBillMonitorEntity playBillMonitorEntity=playBillMonitorService.findByTimeLevelAndPlayBillId(timeLevel,playbillEntity.getId());
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timeLevelS=simpleDateFormat.format(timeLevel);
		R<PlayBillVo> list=remoteDispenseCongfigService.findByPlayBillId(playbillEntity.getId(), SecurityConstants.FROM_IN);
		Set<DispenseMonitorEntity> dispenseMonitors=new HashSet<>();
		int num=0;
		if(playBillMonitorEntity==null){
			playBillMonitorEntity = new PlayBillMonitorEntity();
		}
		if(null!=list.getData()){
			List<DispenseCongfigEntity> dispenses=list.getData().getDispenses();
			List<ConfigDecode> decodes= list.getData().getDecodes();
			if(dispenses!=null){
				dispenses.forEach(dispense->{
					DispenseMonitorEntity dispenseMonitorEntity=new DispenseMonitorEntity();
					dispenseMonitorEntity.setDispenseShouldNum(dispense.getMinFileNum());
					dispenseMonitorEntity.setFtpId(dispense.getFtpId());
					dispenseMonitorEntity.setPlayBillMonitorId(playbillEntity.getId());
					this.getDispenseNum(playbillEntity.getId(),timeLevel.getTime(),dispense.getFtpId(),dispenseMonitorEntity);
					dispenseMonitors.add(dispenseMonitorEntity);
				});
			}
			if(null!=decodes&&decodes.size()>0){
				num=playbillEntity.getMinFileNum();
				if(null!=playbillEntity.getRecs()&&playbillEntity.getRecs().size()>0){
					for(ReceivableEntity receivableEntity:playbillEntity.getRecs()){
						if(receivableEntity.getTimeLevel().equals(timeLevelS)){
							num=receivableEntity.getMinFileNum();
						}
					}
				}
				this.getDecodeNum(playbillEntity.getId(),timeLevel.getTime(),playBillMonitorEntity);
				this.getGatherNum(playbillEntity.getId(),timeLevel.getTime(),playBillMonitorEntity);
			}

		}

		playBillMonitorEntity.setPlayBillId(playbillEntity.getId());
		playBillMonitorEntity.setDataName(playbillEntity.getDataName());
		//playBillMonitorEntity.setDispenseShouldNum(num);
		playBillMonitorEntity.setTimeLevel(timeLevel);
		playBillMonitorEntity.setDatabaseId(playbillEntity.getDatabaseId());
		playBillMonitorEntity.setTableName(playbillEntity.getTableName());
		playBillMonitorEntity.setDdataClassId(playbillEntity.getDdataClassId());
		playBillMonitorEntity.setDdataId(playbillEntity.getDdataId());

		playBillMonitorEntity.setDispenseMonitors(dispenseMonitors);
		playBillMonitorEntity.setCollectShouldNum(num);
		playBillMonitorEntity.setDecodeShouldNum(num);
		if(dispenseMonitors.size()>0){
			playBillMonitorEntity.setDispenseMonitors(dispenseMonitors);
		}


		playBillMonitorService.save(playBillMonitorEntity);
	}
	public void getDispenseNum(Long playBillId,long whenTime,long ftpId,DispenseMonitorEntity dispenseMonitorEntity){
		String statisticalNum=STATISTICAL_NUM+playBillId+":"+whenTime+":"+ftpId;
		try {
			Integer num= (Integer) redisUtil.hget(statisticalNum,"DISPENSE");
			if(null!=num)
			dispenseMonitorEntity.setDispenseRealNum(num);
		} catch (Exception e) {
			return;
		}

	}

	public void getDecodeNum(Long playBillId,long whenTime,PlayBillMonitorEntity playBillMonitorEntity){
		String statisticalNum=STATISTICAL_NUM+playBillId+":"+whenTime;
		try {
			Integer num= (Integer) redisUtil.hget(statisticalNum,"DECODE");
			if(null!=num)
			playBillMonitorEntity.setDecodeRealNum(num);
		} catch (Exception e) {
			return;
		}
	}

	public void getGatherNum(Long playBillId,long whenTime,PlayBillMonitorEntity playBillMonitorEntity){
		String statisticalNum=STATISTICAL_NUM+playBillId+":"+whenTime;
		try {
			Integer num= (Integer) redisUtil.hget(statisticalNum,"GATHER");
			if(null!=num)
			playBillMonitorEntity.setCollectShouldNum(num);
		} catch (Exception e) {
			return;
		}
	}
}
